#!/bin/bash
apt-get install -y gcc g++ make cmake cmake-curses-gui autoconf

apt-get purge needrestart -y
# 进入上级目录
cd ..
# ffmpeg 安装路径
export ipath=/mnt/nas/work/sdk/ffmpeg
rm -rf $ipath
# 设置安装包执行路径
export PATH=$PATH:$ipath/bin
# 设置动态库路径
export PKG_CONFIG_PATH=$ipath/lib/pkgconfig
# 设置编译主目录路径
export homeMake=$(pwd)
#创建编译目录
mkdir -p $homeMake/build
cd $homeMake/build/

# 1 nasm/yasm not found or too old. Use --disable-x86asm for a crippled build
tarp=$homeMake/tar/yasm-1.3.0.tar.gz
rm -rf yasm-1.3.0
#  解压  yasm-1.3.0
tar -xvf $tarp
# 进入   yasm-1.3.0  目录
cd yasm-1.3.0
# 编译 yasm 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf yasm-1.3.0

# 2 Libnettle 3.6 was not found
apt-get install -y libbluray-dev
# Libhogweed (nettle's companion library) 3.6 was not found. Note that you must compile nettle with gmp support
apt install -y libgnutls28-dev
#  Libunistring was not found. To use the included one, use --with-included-unistring
apt-get install -y libunistring-dev

# 3 ERROR: gnutls not found using pkg-config
tarp=$homeMake/tar/gnutls-3.7.0.tar.xz
rm -rf gnutls-3.7.0
#  解压  gnutls-3.7.0
tar -xvf $tarp
# 进入   gnutls-3.7.0  目录
cd gnutls-3.7.0
# 编译 gnutls-3.7.0 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf gnutls-3.7.0

# 4 ERROR: libfdk_aac not found
tarp=$homeMake/tar/fdk-aac-2.0.3.tar.gz
rm -rf fdk-aac-2.0.3
#  解压  libfdk_aac
tar -xvf $tarp
# 进入   libfdk_aac  目录
cd fdk-aac-2.0.3
# 编译 libfdk_aac 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf fdk-aac-2.0.3

# 5 ERROR: libmp3lame >= 3.98.3 not found
tarp=$homeMake/tar/lame-3.100.tar.gz
rm -rf lame-3.100
#  解压  lame-3.100
tar -xvf $tarp
# 进入  lame-3.100 目录
cd lame-3.100
# 编译 yasm 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf lame-3.100

# 6 ERROR: speex not found using pkg-config
tarp=$homeMake/tar/speex-1.2.1.tar.gz
rm -rf speex-1.2.1
#  解压  speex-1.2.1
tar -xvf $tarp
# 进入  speex-1.2.1 目录
cd speex-1.2.1
# 编译 yasm 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf speex-1.2.1

# 7 ERROR: libx264 not found
tarp=$homeMake/tar/x264.tar.gz
rm -rf x264
#  解压  x264
tar -xvf $tarp
# 进入  x264 目录
cd x264
# 编译 x264 配置
./configure --prefix=$ipath  --disable-asm --enable-shared --disable-static
make -j4
make install
cd ..
rm -rf x264

# 8 ERROR: x265 not found using pkg-config
tarp=$homeMake/tar/x265.tar.gz
rm -rf x265
#  解压  x265
tar -xvf $tarp
# 进入  x265 目录
cd x265/build/linux
# 编译 x265 配置
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$ipath ../../source
make -j4
make install
cd ..
rm -rf x265
# 编译 ffmpeg
tarp=$homeMake/tar/ffmpeg-4.4.4.tar.gz
rm -rf ffmpeg-4.4.4
#  解压  ffmpeg
tar -xvf $tarp
# 进入  ffmpeg  目录
cd ffmpeg-4.4.4/
# 配置
./configure \
    --prefix=$ipath \
	  --enable-static \
	  --disable-shared \
 		--enable-gpl \
 		--enable-nonfree \
 		--enable-gnutls \
 		--enable-libmp3lame \
    --enable-libfdk-aac \
    --enable-libx264 \
    --enable-libx265 \
    --enable-filter=delogo \
    --enable-debug \
    --enable-optimizations \
    --enable-libspeex \
    --enable-pthreads \
    --enable-version3 \
    --enable-hardcoded-tables \
    --disable-doc \
    --extra-cflags="-I$ipath/include"  \
    --extra-ldflags="-L$ipath/lib" \
    --arch=x86_64

    make clean
    make -j4
    make install

cd ..
rm -rf $homeMake/build
# 配置动态库位置 $ipath
echo "$ipath/lib" >> /etc/ld.so.conf
# 刷新动态库配置
ldconfig
ffmpeg -version





